home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / demobook / UxXt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  12.0 KB  |  456 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*************************************************************************
  18.  * $Date: 1992/11/23 18:01:54 $
  19.  * $Revision: 1.3 $
  20.  *
  21.  * Copyright (c) 1991, Visual Edge Software Ltd.
  22.  * ---------------------------------------------
  23.  * ALL RIGHTS RESERVED.  This notice is  intended  as  a  precaution
  24.  * against  inadvertent publication,  and shall not be deemed to
  25.  * constitute an acknowledgment that publication has occurred nor to
  26.  * imply any waiver of confidentiality. The year included in the
  27.  * notice is the year of the creation of the work.
  28.  *------------------------------------------------------------------------
  29.  *                UxXt.c                                    
  30.  *------------------------------------------------------------------------*/
  31.  
  32. #include <stdio.h>
  33.  
  34. #include <X11/Intrinsic.h>
  35. #include <X11/Shell.h>
  36. #include <X11/StringDefs.h>
  37. #include <X11/Xutil.h>
  38. #include <X11/X.h>
  39. #include <X11/Xmd.h>
  40. #include <X11/Xlib.h>
  41. #include <Xm/Xm.h>
  42. #include <Xm/DialogS.h>
  43.  
  44. #include "UxXt.h"
  45. #ifdef DEBUG
  46. #include <malloc.h>
  47. #endif
  48.  
  49. static    XContext    xcontext_id = NULL;
  50.  
  51. /******************************************************************************
  52. NAME:        GetTrueToplevel( wgt )
  53.  
  54. INPUT:        Widget    wgt        - the top user-created widget 
  55.  
  56. RETURN:        Widget            - the shell widget
  57.  
  58. DESCRIPTION:    Returns the true toplevel of that interface.
  59.         This is needed for implicit shells where the widget passed to
  60.         one of the Ux functions is not always the shell.
  61.  
  62. CREATION:    Visual Edge Software        April 6 1991
  63. -----------------------------------------------------------------------------*/
  64. #ifdef _NO_PROTO
  65. static    Widget     GetTrueToplevel( wgt )
  66.     Widget     wgt;
  67. #else
  68. static    Widget     GetTrueToplevel( Widget wgt )
  69. #endif
  70. {
  71.     if (wgt && !XtIsShell(wgt)) 
  72.     {
  73.         wgt = XtParent(wgt);
  74.     }
  75.     return wgt;
  76. }
  77.  
  78. /******************************************************************************
  79. NAME:        handle_dialog_child( wgt, manage_func )
  80.  
  81. INPUT:        Widget    wgt            - the dialogShellWidget
  82.         void    (*manage_func)()    - either XtManageChild
  83.                           or XtUnmanageChild
  84.  
  85. RETURN:        int                - UX_NO_ERROR if successfull
  86.                           UX_ERROR otherwise
  87.  
  88. DESCRIPTION:    Handles the popping up or popping down of dialog shells
  89.         by managing or unmanaging their children.
  90.  
  91. CREATION:    Visual Edge Software        Sept 19/91   
  92. -----------------------------------------------------------------------------*/
  93. #ifdef _NO_PROTO
  94. static    int    handle_dialog_child( wgt, manage_func )
  95.     Widget    wgt;
  96.     void    (*manage_func)();
  97. #else
  98. static    int    handle_dialog_child( Widget wgt, void (*manage_func)() )
  99. #endif
  100. {
  101.     int    i, num_children;
  102.     Widget    *children;
  103.  
  104.     XtVaGetValues( wgt,
  105.                XmNnumChildren, &num_children,
  106.                XmNchildren, &children,
  107.                NULL );
  108.  
  109.     /* We manage/unmanage the first rectObj child in the list.
  110.      * Note that the check for rectObjClass is necessary since some
  111.      * implementations of Motif add protocol children to the dialogShell.
  112.      */
  113.  
  114.     for (i = 0; i < num_children; i++)
  115.     {
  116.         if ( XtIsSubclass( children[i], rectObjClass ) )
  117.         {
  118.             (*manage_func)(children[i]);
  119.             return ( UX_NO_ERROR );
  120.         }
  121.     }
  122.  
  123.     return ( UX_ERROR );
  124. }
  125.  
  126. /******************************************************************************
  127. NAME:        popup_dialog( wgt, grab_flag )
  128.  
  129. INPUT:        Widget        wgt        - dialogShell to pop up
  130.         XtGrabKind    grab_flag    - the grab flag
  131.  
  132. RETURN:        void
  133.  
  134. DESCRIPTION:    Pops up a dialogShell.
  135.  
  136. CREATION:    Visual Edge Software        Sept 19/91   
  137. -----------------------------------------------------------------------------*/
  138. #ifdef _NO_PROTO
  139. static    void    popup_dialog( wgt, grab_flag )
  140.     Widget        wgt;
  141.     XtGrabKind    grab_flag;
  142. #else
  143. static    void    popup_dialog( Widget wgt, XtGrabKind grab_flag )
  144. #endif
  145. {
  146.     if ( handle_dialog_child( wgt, XtManageChild ) == UX_ERROR )
  147.         XtPopup( wgt, grab_flag );
  148. }
  149.  
  150. /******************************************************************************
  151. NAME:        UxPopupInterface( wgt, grab_flag )
  152.  
  153. INPUT:        Widget        wgt        - Widget to popup
  154.         XtGrabKind    grab_flag    - grab flag 
  155.  
  156. RETURN:        int            UX_ERROR or UX_NO_ERROR
  157.  
  158. DESCRIPTION:    Popups up an interface. The widget should be a toplevel widget.
  159.         Note that special handling is required for dialogShells since
  160.         those are popped up by managing their children if they have
  161.         some.
  162.         The grab_flag could be any of:
  163.                 no_grab    (XtGrabNone)
  164.                 nonexclusive_grab (XtGrabNonexclusive)
  165.                 exclusive_grab (XtGrabExclusive)
  166.  
  167. CREATION:    Visual Edge Software        April 6 1991
  168. -----------------------------------------------------------------------------*/
  169. #ifdef _NO_PROTO
  170. int    UxPopupInterface( wgt, grab_flag )
  171.     Widget        wgt;
  172.     XtGrabKind    grab_flag;
  173. #else
  174. int    UxPopupInterface( Widget wgt, XtGrabKind grab_flag )
  175. #endif
  176. {
  177.     if (!(wgt = GetTrueToplevel(wgt)))
  178.         return ( UX_ERROR );
  179.  
  180.     if ( XtIsSubclass( wgt, xmDialogShellWidgetClass ) )
  181.     {
  182.         popup_dialog( wgt, grab_flag );
  183.     }
  184.     else
  185.     {
  186.         XtPopup( wgt, grab_flag );
  187.     }
  188.  
  189.     return ( UX_NO_ERROR );
  190. }
  191.  
  192. /******************************************************************************
  193. NAME:        popdown_dialog( wgt )
  194.  
  195. INPUT:        Widget    wgt        - dialogShell to popdown
  196.  
  197. RETURN:        void
  198.  
  199. DESCRIPTION:    Pops down a dialogShell.
  200.  
  201. CREATION:    Visual Edge Software        Sept 19/91   
  202. -----------------------------------------------------------------------------*/
  203. #ifdef _NO_PROTO
  204. static    void    popdown_dialog( wgt )
  205.     Widget    wgt;
  206. #else
  207. static    void    popdown_dialog( Widget wgt )
  208. #endif
  209. {
  210.     if ( handle_dialog_child( wgt, XtUnmanageChild ) == UX_ERROR )
  211.         XtPopdown( wgt );
  212. }
  213.  
  214. /******************************************************************************
  215. NAME:        UxPopdownInterface( wgt )
  216.  
  217. INPUT:        Widget    wgt        - Widget to popdown
  218.  
  219. RETURN:        int            UX_ERROR / UX_NO_ERROR
  220.  
  221. DESCRIPTION:    Pops down an interface. The widget should be a toplevel widget.
  222.         Note that special handling is required for dialogShells since
  223.         those are popped down by unmanaging their children if they have
  224.         some.
  225.  
  226. CREATION:    Visual Edge Software        April 6 1991
  227. -----------------------------------------------------------------------------*/
  228. #ifdef _NO_PROTO
  229. int    UxPopdownInterface( wgt )
  230.     Widget    wgt;
  231. #else
  232. int    UxPopdownInterface( Widget wgt )
  233. #endif
  234. {
  235.     if (!(wgt = GetTrueToplevel(wgt)))
  236.         return ( UX_ERROR );
  237.  
  238.     if ( XtIsSubclass( wgt, xmDialogShellWidgetClass ) )
  239.     {
  240.         popdown_dialog( wgt );
  241.     }
  242.     else
  243.     {
  244.         XtPopdown( wgt );
  245.     }
  246.  
  247.     return ( UX_NO_ERROR );
  248. }
  249.  
  250. /******************************************************************************
  251. NAME:        UxDestroyInterface( wgt )
  252.  
  253. INPUT:        Widget    wgt        - Widget to destroy
  254.  
  255. RETURN:        int            UX_ERROR / UX_NO_ERROR
  256.  
  257. DESCRIPTION:    Destroys an interface.  The widget should be a toplevel widget.
  258.         
  259. CREATION:    Visual Edge Software        April 6 1991
  260. -----------------------------------------------------------------------------*/
  261. #ifdef _NO_PROTO
  262. int    UxDestroyInterface( wgt )
  263.     Widget    wgt;
  264. #else
  265. int    UxDestroyInterface( Widget wgt )
  266. #endif
  267. {
  268.     if (!(wgt = GetTrueToplevel(wgt)))
  269.         return ( UX_ERROR );
  270.  
  271.     XtDestroyWidget (wgt);
  272.  
  273.     return ( UX_NO_ERROR );
  274. }
  275.  
  276. /******************************************************************************
  277. NAME:        DeleteContextCB( wgt, client_data, call_data )
  278.  
  279. INPUT:        Widget        wgt        - widget causing the callback
  280.         XtPointer       client_data    - not used
  281.         XtPointer    call_data    - not used
  282.  
  283. RETURN:        void
  284.  
  285. DESCRIPTION:    Deletes the X context entry.
  286.  
  287. EXT REFERENCES:    UxTopLevel, xcontext_id
  288.  
  289. CREATION:    Visual Edge Software        April 6 1991
  290. -----------------------------------------------------------------------------*/
  291. #ifdef _NO_PROTO
  292. static    void    DeleteContextCB( wgt, client_data, call_data )
  293.     Widget          wgt;
  294.     XtPointer       client_data, call_data;
  295. #else
  296. static    void    DeleteContextCB( Widget wgt, XtPointer client_data,
  297.                         XtPointer call_data )
  298. #endif
  299. {
  300.     (void) XDeleteContext( XtDisplay( DBtoplevel ),
  301.                    (Window) wgt,
  302.                    xcontext_id );
  303. }
  304.  
  305. /******************************************************************************
  306. NAME:        UxPutContext( wgt, context )
  307.  
  308. INPUT:        Widget    wgt        - Widget
  309.         caddr_t    context        - context pointer
  310.  
  311. RETURN:        int            UX_ERROR / UX_NO_ERROR
  312.  
  313. DESCRIPTION:    Uses the X Context manager to store the given context pointer
  314.         in a memory location that is indexed by the given widget id.
  315.         Also adds a destroyCallback to delete that context when the
  316.         widget is destroyed.
  317.  
  318. EXT REFERENCES:    UxTopLevel, xcontext_id
  319. EXT EFFECTS:    xcontext_id
  320.  
  321. CREATION:    Visual Edge Software        April 6 1991
  322. -----------------------------------------------------------------------------*/
  323. #ifdef _NO_PROTO
  324. int    UxPutContext( wgt, context )
  325.     Widget    wgt;
  326.     caddr_t    context;
  327. #else
  328. int    UxPutContext( Widget wgt, caddr_t context )
  329. #endif
  330. {
  331.     int        status;
  332.  
  333.     if ( xcontext_id == NULL )
  334.         xcontext_id = XUniqueContext();
  335.  
  336.     if ( wgt == NULL )
  337.         return ( UX_ERROR );
  338.  
  339.     status = XSaveContext( XtDisplay( DBtoplevel ), 
  340.                    (Window) wgt, 
  341.                    xcontext_id, 
  342.                    context );
  343.     if ( status != 0 )
  344.         return ( UX_ERROR );
  345.  
  346.     XtAddCallback (wgt, XmNdestroyCallback, DeleteContextCB, NULL);
  347.  
  348.     return ( UX_NO_ERROR );
  349. }
  350.  
  351. /******************************************************************************
  352. NAME:        UxGetContext( wgt )
  353.  
  354. INPUT:        Widget    wgt        - widget
  355.  
  356. RETURN:        caddr_t            - the context pointer
  357.  
  358. DESCRIPTION:    Uses the X Context manager to find the context pointer
  359.         stored in a memory location indexed by the given widget id.
  360.  
  361. EXT REFERENCES:    UxTopLevel, xcontext_id
  362.  
  363. CREATION:    Visual Edge Software        April 6 1991
  364. -----------------------------------------------------------------------------*/
  365. #ifdef _NO_PROTO
  366. caddr_t    UxGetContext( wgt )
  367.     Widget    wgt;
  368. #else
  369. caddr_t    UxGetContext( Widget wgt )
  370. #endif
  371. {
  372.     int    status;
  373.     caddr_t    context;
  374.  
  375.     if ( wgt == NULL )
  376.         return ( (caddr_t) NULL );
  377.  
  378.     status = XFindContext( XtDisplay( DBtoplevel ), 
  379.                    (Window) wgt, 
  380.                    xcontext_id, 
  381.                    &context );
  382.  
  383.     if ( status != 0 )
  384.         return    ( (caddr_t) NULL );
  385.  
  386.     return ( context );
  387. }
  388.  
  389. /******************************************************************************
  390. NAME:        UxFreeClientDataCB( wgt, client_data, call_data )
  391.  
  392. INPUT:        Widget        wgt        - widget
  393.         XtPointer       client_data    - pointer to be freed
  394.         XtPointer    call_data    - not used
  395.  
  396. RETURN:        void
  397.  
  398. DESCRIPTION:    This callback function simply frees the client data.
  399.  
  400. CREATION:    Visual Edge Software        April 6 1991
  401. -----------------------------------------------------------------------------*/
  402. #ifdef _NO_PROTO
  403. void    UxFreeClientDataCB( wgt, client_data, call_data )
  404.     Widget          wgt;
  405.     XtPointer       client_data, call_data;
  406. #else
  407. void    UxFreeClientDataCB( Widget wgt, XtPointer client_data,
  408.                 XtPointer call_data )
  409. #endif
  410. {
  411.     if (client_data != NULL)
  412.         XtFree(client_data);
  413. }
  414.  
  415. /******************************************************************************
  416. NAME:        UxLoadResources( fname )
  417.  
  418. INPUT:        char    *fname          - name of resource file
  419.  
  420. RETURN:        void
  421.  
  422. DESCRIPTION:    This function is provided as a stub function to load the
  423.         resource file that was generated by the C Code Generator
  424.         into the current toolkit resource database.
  425.         A call to this function is automatically generated with the
  426.         appropriate resource filename when resources are set to Public.
  427.  
  428. CREATION:    Visual Edge Software        April 6 1991
  429. -----------------------------------------------------------------------------*/
  430. #ifdef _NO_PROTO
  431. void    UxLoadResources( fname )
  432.     char    *fname;
  433. #else
  434. void    UxLoadResources( char *fname )
  435. #endif
  436. {
  437. }
  438.  
  439. void    UxNotify()
  440. {
  441.     UxNotifyFlag = False;
  442. }
  443.  
  444. void    UxWaitForNotify()
  445. {
  446.     XEvent event;
  447.     UxNotifyFlag = True;
  448.     while (UxNotifyFlag) {
  449.       XtAppNextEvent(DBapp_context, &event);
  450.       XtDispatchEvent(&event);
  451. /*      printf ("UxNotifyFlag = %d\n", UxNotifyFlag); */
  452.     }
  453. }
  454. /***END*OF*FILE****************************************************************/
  455.  
  456.